package cn.ncupyh.comptest;

import static java.lang.Math.pow;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/7/26 10:50
 * @注释:
 */
public class Palindrome {
    //不推荐
    public static boolean isPalindrome1(int x) {
        if(x<0){
            return false;
        }
        int tmp = x;
        int ret = 0;
        int n = 0;
        //先计算数字的位数
        while (tmp != 0) {
            n++;
            tmp /= 10;
        }
        //将原来的数字一半倒过来
        int left = n;
        int right = 1;
        ret = x;
        while (right < left) {
            int m = (ret / (int)pow(10, right - 1) % 10);
            int p = (ret / (int)pow(10, left - 1) % 10);
            if (m != p) {
                return false;
            }
            right++;
            left--;
        }
        return true;

    }

    public static boolean isPalindrome2(int x) {
        if(x < 0)
            return false;
        int cur = 0;
        int num = x;
        while(num != 0) {
            cur = cur * 10 + num % 10;
            num /= 10;
        }
        return cur == x;
    }
    public static void main(String[] args) {
        boolean b = isPalindrome2(1001);
        System.out.println(b)
        ;
    }
}

